package com.wangba.controller;

import com.wangba.pojo.Member;
import com.wangba.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService mermberService;

    //查询全部的会员
    @RequestMapping("/allMember")
    public String list(Model model) {
        List<Member> list = mermberService.queryAllMember();
        model.addAttribute("list", list);
        return "allMember";
    }
    //跳转到增加会员页面
    @RequestMapping("/toAddMember")
    public String toAddPaper() {
        return "addMember";
    }
    //增加会员
    @RequestMapping("/addMember")
    public String addPaper(Member member) {
        System.out.println(member);
        mermberService.addMember(member);
        return "redirect:/member/allMember";
    }

    //RestFul风格，删除会员
    @RequestMapping("/del/{memberbookID}")
    public String deleteMember(@PathVariable(value = "memberbookID") int id) {
        mermberService.deleteMemberById(id);
        return "redirect:/member/allMember";
    }
   /**
     通过写 增删改查 发现增删查中除了删除需要加参数其他两种都很好实现，
     但是只有修改数据这个比较麻烦 首先第一个问题就是容易遗忘id，
     因为在写修改页面的时候，id往往是隐藏表单实现的，由于是隐藏起来的
     所以就容易忘记，导致修改功能失败，起初以为是事务的问题，但是
    转念一想，在实现其他功能也没有提交失败，所以这个错误经常犯
    在此记录一下，写完会员的增删改查，那么临时机的增删改查就是复制粘贴！！！！
    **/
    @RequestMapping("/toUpdateMember")
    public String toUpdateMember(Model model, int id) {
        Member member = mermberService.queryMemberById(id);
        System.out.println(member);
        model.addAttribute("member", member);
        return "updateMember";
    }

    @RequestMapping("/updateMember")
    public String updateMember(Model model, Member member) {
        System.out.println(member);
        Member memberSearch = mermberService.queryMemberById(member.getMemberID());
        //通过用BigDecimal来实现对小数的加减
        BigDecimal a1 = new BigDecimal(memberSearch.getMemberCost());
        BigDecimal a2 = new BigDecimal(member.getMemberCost());
        BigDecimal newCost = a1.add(a2);
        //DecimalFormat保留一位小数  来源：https://blog.csdn.net/qq_30336433/article/details/81298318
        member.setMemberCost(new DecimalFormat("##.##").format(newCost));

        mermberService.updateMember(member);
        Member memberUpdate = mermberService.queryMemberById(member.getMemberID());
        model.addAttribute("member", memberUpdate);
        return "redirect:/member/allMember";
    }

    @RequestMapping("/queryMember")
    public String querMember(String queryMemberName, Model model) {
        Member member = mermberService.queryMemberByName(queryMemberName);
        List<Member> list = new ArrayList<>();
        list.add(member);
        /*if (member == null) {
            list = mermberService.queryAllMember();
            model.addAttribute("error", "未查到");
        }*/
        model.addAttribute("list", list);
        return "allMember";
    }


}





